package cn.tedu._08weibo.controller;

import cn.tedu._08weibo.base.response.Result;
import cn.tedu._08weibo.base.response.StatusCode;
import cn.tedu._08weibo.mapper.CommentMapper;
import cn.tedu._08weibo.pojo.dto.CommentAddParam;
import cn.tedu._08weibo.pojo.entity.Comment;
import cn.tedu._08weibo.pojo.vo.CommentVO;
import cn.tedu._08weibo.pojo.vo.UserVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * @author tune_the last
 * @date 2025/11/17 - 18:42
 */
@Slf4j
@Tag(name = "评论模块")
@RestController
@RequestMapping("/v1/comment/insert")
public class CommentController {

    @Autowired
    private CommentMapper commentMapper;


    @ApiOperationSupport(order = 10)
    @Operation(summary = "发表评论")
    @PostMapping("/insert")
    public Result insertComment(@RequestBody CommentAddParam commentAddParam, HttpSession session){
        log.debug("commentAddParam：" + commentAddParam);
        UserVO userVO = (UserVO) session.getAttribute("user");
        if (userVO == null){
            return Result.fail(StatusCode.NOT_LOGIN);
        }
        Comment comment = new Comment();
        BeanUtils.copyProperties(commentAddParam, comment);
        comment.setUserId(userVO.getId());
        comment.setCreateTime(new Date());
        log.debug("复制后的值为：" + comment);
        int i = commentMapper.insertComment(comment);
        if(i > 0){
            return Result.ok();
        }
        return Result.fail();
    }

    @ApiOperationSupport(order = 20)
    @Operation(summary = "获取评论列表")
    @PostMapping("/selectByWeiboId")
    public Result commentList(Long weiboId){
        log.debug("weiboId：" + weiboId);
        log.debug("你好");
        List<CommentVO> list = commentMapper.selectByWeiboId(weiboId);
        return Result.ok(list);
    }

}
